package Utilities;

public class DSAUtilities {

	
	/**
	 * Check that the (L, N) pair is in the list of acceptable 
	 * @param L The length of the prime p
	 * @param N The length of the prime q
	 * @return true if the pair is in the list and else
	 */
	public static boolean checkValid(int L, int N){
		return ((L == 1024 && N == 160) || (L == 2048 && N == 224) || (L == 2048 && N == 256) || (L == 3072 && N == 256));
	}
	
	/**
	 * Find the approved hash function which has the security strength acceptable
	 * @param N The desired length of the prime q (in bits)
	 * @return The name of the hash function
	 */
	public static String approvedHashFunction(int N){
		switch (N) {
			case 160:
				return "SHA-1";
			case 224:
				return "SHA-224";
			case 256:
				return "SHA-256";
			default:
				return null;
		}
	}
	
	/**
	 * Find the length of the output of a hash function
	 * @param hashfunction Name of hash function (SHA-1, SHA-224...)
	 * @return
	 */
	public static int findOutlen(String hashfunction){
		switch (hashfunction) {
		case "SHA-1":
			return 160;
		case "SHA-224":
			return 224;
		case "SHA-256":
			return 256;
		case "SHA-384":
			return 384;
		default:
			return 0;
		}
	}

	public static byte[] concatByteArray(byte[] b1, byte[] b2){
		byte[] result = new byte[b1.length + b2.length];
		int i;
		for (i = 0; i < b1.length; i++) {
			result[i] = b1[i];
		}
		for (i = 0; i < b2.length; i++){
			result[i + b1.length] = b2[i];
		}
		return result;
	}
	
	public static final byte[] intToByteArray(int value) {
	    return new byte[] {
	            (byte)(value >>> 24),
	            (byte)(value >>> 16),
	            (byte)(value >>> 8),
	            (byte)value};
	}
	
	/**
	 * [Reference] NIST SP800-57 Section 5.6.1 Algorithm Strengths
	 * @param L FFC public key size
	 * @param N FFC private key size
	 * @return The number of bits of security provided by the DSA algorithms and associated with the (L, N) pair
	 */
	public static int getBitofSecurityStrength(int L, int N){
		if (L == 1024 && N == 160){
			return 80;
		}
		
		if (L == 2048 && N == 224){
			return 112;
		}
		
		if (L == 3072 && N == 256){
			return 128;
		}
		
		if (L == 7680 && N == 384){
			return 192;
		}
		
		if (L == 15360 && N == 512){
			return 256;
		}
		return -1;
	}
	
	public static void main(String[] args) {
		// TODO Auto-generated method stub

	}

}
